/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.text.DecimalFormat;
import java.util.List;

import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.entity.Planning;
import com.jeeplus.modules.tms.mapper.PlanningMapper;
import com.jeeplus.modules.tms.entity.Informations;
import com.jeeplus.modules.tms.mapper.InformationsMapper;

/**
 * 委托单Service
 * @author 李超辉
 * @version 2017-12-11
 */
@Service
@Transactional(readOnly = true)
public class PlanningService extends CrudService<PlanningMapper, Planning> {

	@Autowired
	private InformationsMapper informationsMapper;
	@Autowired
	private GoodsService service;
	
	public Planning get(String id) {
		Planning planning = super.get(id);
		planning.setInformationsList(informationsMapper.findList(new Informations(planning)));
		return planning;
	}
	
	public List<Planning> findList(Planning planning) {
		return super.findList(planning);
	}
	
	public Page<Planning> findPage(Page<Planning> page, Planning planning) {
		return super.findPage(page, planning);
	}
	
	@Transactional(readOnly = false)
	public void save(Planning planning) {
		super.save(planning);
		for (Informations informations : planning.getInformationsList()){
			if (informations.getId() == null){
				continue;
			}
			if (Informations.DEL_FLAG_NORMAL.equals(informations.getDelFlag())){
				if (StringUtils.isBlank(informations.getId())){
					informations.setPlanning(planning);
					informations.preInsert();
					informationsMapper.insert(informations);
				}else{
					informations.preUpdate();
					informationsMapper.update(informations);
				}
			}else{
				informationsMapper.delete(informations);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Planning planning) {
		super.delete(planning);
		informationsMapper.delete(new Informations(planning));
	}
	
}